import SwiftUI
import Lottie

struct ResultView: View {
    @Environment(\.presentationMode) var presentationMode
    @EnvironmentObject var speedViewModel: SpeedViewModel
    @EnvironmentObject var timerModel: TimerViewModel
    @State private var isSpeed = false
    @State private var isButtonEnabled = false
    @State private var showLoadingView = true

    var body: some View {
        NavigationView {
            ZStack {
                MainView()

                VStack {
                    Back(title: "Result", onBackTap: {
                        self.presentationMode.wrappedValue.dismiss()
                    }, onImageTap: {})
                    
                    Image("mainimg")
                        .resizable(resizingMode: .stretch)
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 230, height: 230)
                     
                    // Lottie 动画
                    if showLoadingView {
                        LoadingView(lottie: "lo", content: "Initializing...")
                          
                    }
                    
                    // 按钮，初始时隐藏
                    Button(action: {
                        isSpeed.toggle()
                    }) {
                        HStack {
                            Image("net")
                            Spacer()
                            Text("Speed Test")
                                .foregroundColor(Color(hex: "#33F3E3"))
                            Spacer()
                            Image("left")
                                .padding(.trailing, 10)
                        }
                       
                        .background(Color("sp"))
                        .cornerRadius(20)
                        .padding(.horizontal)
                    }
                    .disabled(!isButtonEnabled)
                    .opacity(isButtonEnabled ? 1.0 : 0.0)  // 按钮初始时透明
                    .animation(.easeIn(duration: 0.5), value: isButtonEnabled)  // 按钮出现时的动画效果
                    
                    Spacer()
                }

                NavigationLink(destination: NetSpeedView()
                                .environmentObject(speedViewModel)
                                .environmentObject(timerModel),
                               isActive: $isSpeed) {
                    EmptyView()
                }
                .hidden()
            }
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
        }
        .navigationBarHidden(true)
        .navigationViewStyle(StackNavigationViewStyle())
        .onAppear {
            DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                showLoadingView = false  // 隐藏动画
                isButtonEnabled = true  // 启用按钮
            }
        }
    }
}

#Preview{
    ResultView()
}



struct LoadingView: View {
   var lottie:String
   var content:String
   var body: some View {
       VStack {
           LottiePlayer(name: lottie)
               .frame(width: 180, height: 180)
               .padding()
           Text(content)
               .bold()
               .padding(.top,-10)
               .foregroundColor(.white)
           Spacer()
       }
       .frame(width: 250, height: 250)
       .background(Color("sp"))
      
       .cornerRadius(20)
       .shadow(radius: 10)
   }



struct LottiePlayer: UIViewRepresentable {
   let name: String
   let animationSpeed: CGFloat
   let loopMode: LottieLoopMode
   
   init(
       name: String,
       animationSpeed: CGFloat = 1.0,
       loopMode: LottieLoopMode = .loop
   ) {
       self.name = name
       self.animationSpeed = animationSpeed
       self.loopMode = loopMode
   }
   
   func makeUIView(context: Context) -> UIView {
       let view = UIView(frame: .zero)
       let animationView = LottieAnimationView(name: name)
       animationView.loopMode = loopMode
       animationView.animationSpeed = animationSpeed
       animationView.contentMode = .scaleAspectFit
       animationView.play()
       
       animationView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(animationView)
       NSLayoutConstraint.activate([
           animationView.topAnchor.constraint(equalTo: view.topAnchor),
           animationView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
           animationView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
           animationView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
       ])
       
       return view
   }
   
   func updateUIView(_ uiView: UIView, context: Context) {}
}
}



